package servlet;

import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Movie;
import domain.User;
import ex.DatabaseException;

import service.SearchService;


public class Search extends HttpServlet {
	SearchService sservice=new SearchService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String query = request.getParameter("search");
		query = query.trim();
		if(query.equals("")){
			RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/search.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
			return;			
		}
		HashMap<User,Long> userMap = null;
		HashMap<Movie,Long> movieMap = null;

		try {
			userMap = sservice.searchUsers(query);
	        UserValueComparator uvc =  new UserValueComparator(userMap);
	        TreeMap<User,Long> sortedUserMap = new TreeMap<User,Long>(uvc);    
	        sortedUserMap.putAll(userMap);

			movieMap = sservice.searchMovies(query);
	        MovieValueComparator mvc =  new MovieValueComparator(movieMap);
	        TreeMap<Movie,Long> sortedMovieMap = new TreeMap<Movie,Long>(mvc);
	        sortedMovieMap.putAll(movieMap);
			
	        System.out.println("resultssortedUser: "+sortedUserMap);
	        System.out.println("resultsuser: "+userMap);
	        System.out.println("resultssortedMov: "+sortedMovieMap);
	        System.out.println("resultsmov: "+movieMap);
	        
			request.setAttribute("users", sortedUserMap);
			request.setAttribute("movies", sortedMovieMap);
		} catch (DatabaseException e) {
			request.setAttribute("searchError", e.getMessage());
		}catch (Exception e) {
			request.setAttribute("searchError", "A general error occured");
		}
		finally{
			RequestDispatcher dispatcher = request.getRequestDispatcher("jsp/user/search.jsp");
			if (dispatcher != null) dispatcher.forward(request, response);
		}
	}
	
	static class UserValueComparator implements Comparator<User> {

	    Map<User, Long> base;
	    public UserValueComparator(Map<User, Long> base) {
	        this.base = base;
	    }

	    public int compare(User a, User b) {
	        if (base.get(a) >= base.get(b)) {
	            return -1;
	        } else {
	            return 1;
	        } // returning 0 would merge keys
	    }
	}
	
	static class MovieValueComparator implements Comparator<Movie> {

	    Map<Movie, Long> base;
	    public MovieValueComparator(Map<Movie, Long> base) {
	        this.base = base;
	    }

	    // Note: this comparator imposes orderings that are inconsistent with equals.    
	    public int compare(Movie a, Movie b) {
	        if (base.get(a) >= base.get(b)) {
	            return -1;
	        } else {
	            return 1;
	        } // returning 0 would merge keys
	    }
	}

}
